<template>
	<view class="charts-box">
	  <br>
	  <p align="center" style="color:#7cb5ec">本周收益 : {{dayValue}}元   (不含今日)</p>
	  <qiun-data-charts
		type="pie"
		:chartData="chartData"
		background="none"
	  />
	  <p align="center" style="color:#7cb5ec">趋势图</p>
	  <qiun-data-charts
		type="line"
		:chartData="lineData"
		background="none"
		@getIndex="getIndex"
	  />
	</view>
</template>

<script>
    export default {
        data() {
            return {
				dayValue : 0,
				chartData:{
					"series": [
						{
							"data": [
								{
									"name": "test",
									"value": 1
								}
							]
						}
					]
				},
				lineData:{
					"categories": [],
					"series": []
				}
            }
        },
        methods: {
			onLoad() {
				this.getHistoryInfo()
			},
			getIndex(e){
				var index = e.currentIndex.index
				this.getDayInfo(index)
			},
			getDayInfo(index){
				var info = []
				this.dayValue = 0
				console.log(index)
				for(var i in this.lineData["series"]){
					if(this.lineData["series"][i]["name"] != "总收益"){
						var value = this.lineData["series"][i]["data"][index]
						if(value){
							info.push({name : this.lineData["series"][i]["name"],value : value})
							this.dayValue += value
							console.log(this.lineData["series"][i]["name"],value,this.dayValue)
						}
					}
				}
				this.chartData["series"][0]["data"] = info
			},
			getHistoryInfo(){
				this.request("/history_week",{},(res) => {
					if(res.data){
						this.lineData = {"categories":[],"series":[]}
						for(var i = 0;i < res.data.length;i++){
							for(var j = 0;j < res.data[i]["list"].length;j++){
								if(res.data[i]["list"][j])
									res.data[i]["list"][j] = JSON.parse(res.data[i]["list"][j])
							}
							res.data[i]["list"].reverse()
						}
						var allInfo = {
							name : "总收益",
							data : []
						}
						for(var i = 0;i < res.data[0]["list"].length;i++){
							var d = new Date(res.data[0]["list"][i]["date"])
							var str = (d.getMonth() + 1) +"."+d.getDate()
							allInfo.data[i] = 0
							this.lineData["categories"].push(str)
						}
						for(var i = 0;i < res.data.length;i++){
							var info = {}
							info.name = res.data[i]["name"]
							info.data = []
							var num = 0
							if(res.data[i]["list"]){
								for(var j = 0;j < res.data[i]["list"].length;j++){
									if(res.data[i] && res.data[i]["list"][j] && res.data[i]["list"][j]["a_pay_amount"]){
										info.data[j] = res.data[i]["list"][j]["a_pay_amount"]
										allInfo.data[j] += res.data[i]["list"][j]["a_pay_amount"]
										num += allInfo.data[j]
									}else
										info.data[j] = 0
								}
							}
							if(num){
								console.log("info!!!",info.name)
								this.lineData["series"].push(info)
							}
						}
						this.lineData["series"].push(allInfo)
						console.log(this.lineData)
						this.getDayInfo(this.lineData["series"][0]["data"].length-1)
					}
				})
			},
            // request请求
            request(path,options,success) {
				this.$axios.post(path,options)//传参
				.then(function(res){
					  success(res)
				})
				.catch(function(err){
					  console.log("请求失败233");
				});
            }
		}
    }
</script>

<style>
	/* #ifndef H5 */
	.charts-box{
	  width: 100%;
	  height:300px;
	}
	/* #endif */
</style>
